macro(add_test_with_mode exe mode device)
  add_test(NAME ${exe}-${mode} COMMAND ${exe} --verbose --device ${device})
  set_property(TEST ${exe}-${mode} APPEND PROPERTY ENVIRONMENT OCCA_CACHE_DIR=${CMAKE_BINARY_DIR}/cache)
endmacro()

macro(add_test_without_mode exe)
  add_test(NAME ${exe} COMMAND ${exe} --verbose)
  set_property(TEST ${exe} APPEND PROPERTY ENVIRONMENT OCCA_CACHE_DIR=${CMAKE_BINARY_DIR}/cache)
endmacro()

macro(add_test_with_modes exe)
  add_test_with_mode(${exe} serial "mode: 'Serial'")
  if (WITH_CUDA)
    add_test_with_mode(${exe} cuda "mode: 'CUDA', device_id: 0")
  endif()
  if (WITH_HIP)
    add_test_with_mode(${exe} hip "mode: 'HIP', device_id: 0")
  endif()
  if (WITH_METAL)
    add_test_with_mode(${exe} metal "mode: 'Metal', device_id: 0")
  endif()
  if (WITH_OPENCL)
    add_test_with_mode(${exe} opencl "mode: 'OpenCL', platform_id: 0, device_id: 0")
  endif()
  if (WITH_OPENMP)
    add_test_with_mode(${exe} openmp "mode: 'OpenMP'")
  endif()
endmacro()

macro(compile_c_example target file)
  add_executable(examples_c_${target} ${file})
  target_link_libraries(examples_c_${target} libocca)
  if (ENABLE_TESTS)
    add_test_with_modes(examples_c_${target})
  endif()
endmacro()

macro(compile_cpp_example target file)
  add_executable(examples_cpp_${target} ${file})
  target_link_libraries(examples_cpp_${target} libocca)
  if (ENABLE_TESTS)
    add_test_without_mode(examples_cpp_${target})
  endif()
endmacro()

macro(compile_cpp_example_with_modes target file)
  add_executable(examples_cpp_${target} ${file})
  target_link_libraries(examples_cpp_${target} libocca)
  if (ENABLE_TESTS)
    add_test_with_modes(examples_cpp_${target})
  endif()
endmacro()

add_subdirectory(c)
add_subdirectory(cpp)
